const util = require('util');
const debug = util.debuglog('oil-game:box:api');

//增加金币
exports.addCoins = function (session, coins) {
  let {uid} = session;
  debug("uid = %s  coins = %s 增加金币", uid, coins);
  return new Promise((resolve, reject) => {
      this.app.rpc.login.loginRemote.addCoins(session, uid, session.frontendId, coins, true, function (err) {
          if (err) return reject(err);
          resolve();
      });
  });
}

//增加石油
exports.addOil = function (session, changeNum) {
  let {uid, frontendId} = session;
  debug("uid = %s  frontendId = %s  changeNum = %s 增加石油", uid, frontendId, changeNum);
  return new Promise((resolve, reject) => {
    this.app.rpc.login.loginRemote.addOil(session, uid, frontendId, changeNum, "box", '',function (err, data) {
      if (err) {
        debug("uid = %s  增加石油异常  %s", uid, JSON.stringify(err));
        return reject(err);
      }
      debug("uid = %s  增加石油成功，结果为 %s", uid, JSON.stringify(data));
      resolve(data);
    });
  });
}

//获取升级相关数据
exports.getUpgradeDate = function (session) {
  let {uid} = session;
  debug("uid = %s  获取升级相关数据", uid);
  return new Promise((resolve, reject) => {
    this.app.rpc.upgrade.upgradeRemote.getUpgradeDate(session, uid, function (err, data) {
      if (err) {
        debug("uid = %s  获取升级相关数据异常  %s", uid, JSON.stringify(err));
        return reject(err);
      }
      debug("uid = %s  获取升级相关数据成功，结果为 %s", uid, JSON.stringify(data));
      resolve(data);
    });
  });
}

//获取用户相关信息
exports.getUserData = function (session) {
  let {uid} = session;
  debug("uid = %s  获取用户相关数据", uid);
  return new Promise((resolve, reject) => {
    this.app.rpc.login.loginRemote.getUserData(session, uid, function (err, data) {
      if (err) {
        debug("uid = %s  获取用户相关数据异常  %s", uid, JSON.stringify(err));
        return reject(err);
      }
      debug("uid = %s  获取用户相关数据成功，结果为 %s", uid, JSON.stringify(data));
      resolve(data || {});
    });
  });
}
